﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Text;

namespace nEditor
{
    public partial class ZoneMenu
    {
        private static GUIContent
            LoadCsvButtonContent = new GUIContent("转换CSV", "load unit csv file");

        void ZoneFrameUnitcsv2json()
        {
            if (GUILayout.Button(LoadCsvButtonContent))
            {
                LoadUnitData(ZoneConfig.CsvPath + "unit.csv");
                LoadSkillData(ZoneConfig.BattleCsvPath + "skill.csv");
                LoadRoleData(ZoneConfig.RoleCsvPath + "role.csv");
                LoadBuffData(ZoneConfig.BuffCsvPath + "buffer_bt.csv");

            }
        }

        private string[][] Array;

        #region 本阵数据模板
        /// <summary>
        /// 加载所有单位
        /// </summary>
        public void LoadUnitData(string path)
        {
            string[] lines = System.IO.File.ReadAllLines(path, Encoding.UTF8);

            for (int i = 2; i < lines.Length; ++i)
            {
                string[] Array = lines[i].Split(',');

                ArraryToUnitJson(Array);
            }

            AssetDatabase.Refresh();
        }

        void ArraryToUnitJson(string[] Array)
        {
            nUnitData data = new nUnitData();
            data.id = int.Parse(Array[0]);
            data._res_name = Array[3];
            data._unit_name = Array[1];
            data._Width = float.Parse(Array[4]) * ZoneCfg.RhombusMin;
            data._Height = data._Width / ZoneCfg.RhombusFactor;
            data._UnitType = (nUnitType)int.Parse(Array[2]);
            char.TryParse(Array[6], out data._pivot);
            data.spine_name = Array[7];
            data.spine_zone_scale = float.Parse(Array[8]);
            data.spine_fight_scale = float.Parse(Array[9]);

            string content = JsonUtility.ToJson(data, true);
            string path = ZoneConfig.CsvPath + data.id + ".txt";

            var streamWriter = new StreamWriter(path);
            streamWriter.Write(content);
            streamWriter.Close();

        }
        #endregion

        #region 技能数据模板
        public void LoadSkillData(string path)
        {
            string[] lines = System.IO.File.ReadAllLines(path, Encoding.UTF8);

            for (int i = 2; i < lines.Length; ++i)
            {
                string[] Array = lines[i].Split(',');

                ArraryToSkillDataJson(Array);
            }

            AssetDatabase.Refresh();
        }

        void ArraryToSkillDataJson(string[] Array)
        {
            bSkillData data = new bSkillData();
            data.id = Array[0];
            data.name = Array[1];
            data.resname = Array[2];
            data.resskillid = Array[3];
            data.resskilldes = Array[4];

            string content = JsonUtility.ToJson(data, true);
            string path = ZoneConfig.BattleCsvPath + data.id + "_skill.txt";

            var streamWriter = new StreamWriter(path);
            streamWriter.Write(content);
            streamWriter.Close();
        }
        #endregion

        #region 角色模板
        public void LoadRoleData(string path)
        {
            string[] lines = System.IO.File.ReadAllLines(path, Encoding.UTF8);

            for (int i = 2; i < lines.Length; ++i)
            {
                string[] Array = lines[i].Split(',');

                ArraryToRoleDataJson(Array);
            }

            AssetDatabase.Refresh();
        }

        void ArraryToRoleDataJson(string[] Array)
        {
            bRoleData data = new bRoleData();
            data.id = Array[0];
            data.name = Array[1];
            data.speed = float.Parse(Array[3]);
            data.attack = float.Parse(Array[4]);
            data.time_per_round = int.Parse(Array[5]);

            string content = JsonUtility.ToJson(data, true);
            string path = ZoneConfig.RoleCsvPath + data.id + "_role.txt";

            var streamWriter = new StreamWriter(path);
            streamWriter.Write(content);
            streamWriter.Close();
        }
        #endregion

        #region Buff树的配置
        public void LoadBuffData(string path)
        {
            string[] lines = System.IO.File.ReadAllLines(path, Encoding.UTF8);

            string[] Arrayname = lines[0].Split(',');
            string[] Arraytype = lines[1].Split(',');

            for (int i = 2; i < lines.Length; ++i)
            {
                string[] Array = lines[i].Split(',');

                ArraryTobtBufferCfg(i, Array, Arraytype, Arrayname);
            }

            AssetDatabase.Refresh();
        }

        void ArraryTobtBufferCfg(int index, string[] Array, string[] Arraytype, string[] Arrayname)
        {
            btBufferCfg data = new btBufferCfg();
            data.ArrayValue = Array;
            data.ArrayType = Arraytype;
            data.ArrayName = Arrayname;

            string content = JsonUtility.ToJson(data, true);
            string path = ZoneConfig.BuffCsvPath + index.ToString() + "_buffer.txt";

            var streamWriter = new StreamWriter(path);
            streamWriter.Write(content);
            streamWriter.Close();
        }
        #endregion
    }
}
